package com.crazy.multids.controller;

import com.crazy.multids.config.common.DataSourceEnum;
import com.crazy.multids.config.common.DynaminDataSource;
import com.crazy.multids.entity.OrderItem;
import com.crazy.multids.mapper.OrderItemMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.util.Date;

/**
 * 此类的描述是：
 *
 * @author crazyhu@wezhuiyi.com
 * @date 2018-09-16 13:36
 **/
@Slf4j
@RestController
public class DemoController {

    @Autowired
    private OrderItemMapper orderItemMapper;

    @RequestMapping("/demo")
    @DynaminDataSource(DataSourceEnum.WRITE)
    public String demo() {

        OrderItem orderItem = new OrderItem();
        orderItem.setAppId(10052);
        orderItem.setItemName("aaaa");
        orderItem.setCreateTime(new Date());
        orderItem.setUpdateTime(new Date());
        orderItemMapper.insertSelective(orderItem);

        return "ok";
    }

    @RequestMapping("/query")
    @DynaminDataSource(DataSourceEnum.READ)
    public OrderItem query() {
        OrderItem orderItem = orderItemMapper.selectByPrimaryKey(14L);

        return orderItem;
    }


//    @Override
//    public void onApplicationEvent(ApplicationEvent event) {
//        if (event instanceof ContextRefreshedEvent) {
//            ContextRefreshedEvent e = (ContextRefreshedEvent) event;
//            for (String beanName : e.getApplicationContext().getBeanDefinitionNames()) {
//                log.info("--------->" + beanName);
//            }
//        }
//    }
}
